package net.jsoft.platform.schedule.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.quartz.JobDataMap;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import net.jsoft.platform.core.util.platform.PlatformUtils;
import net.jsoft.platform.schedule.entity.Schedule;

@Transactional
@Component
public class ScheduleRepository
{
    @Resource(name = "mysqlJdbcTemplate")
    private JdbcTemplate template;

    public List<Schedule> findAllTriggers()
    {
        String sql = "select t.JOB_NAME, t.JOB_GROUP, t.DESCRIPTION ,t.PREV_FIRE_TIME ,t.NEXT_FIRE_TIME,t.TRIGGER_STATE ,t.START_TIME,ct.CRON_EXPRESSION from QRTZ_TRIGGERS t left join QRTZ_CRON_TRIGGERS ct on t.SCHED_NAME = ct.SCHED_NAME and t.TRIGGER_NAME = ct.TRIGGER_NAME and t.TRIGGER_GROUP = ct.TRIGGER_GROUP order by start_time";
        List<Map<String, Object>> allTriggers = template.queryForList(sql);

        List<Schedule> list = new ArrayList<Schedule>();

        for (Map<String, Object> t : allTriggers)
        {
            Schedule info = new Schedule();
            info.setJobName(t.get("JOB_NAME") == null ? "" : t.get("JOB_NAME").toString());
            info.setJobGroup(t.get("JOB_GROUP") == null ? "" : t.get("JOB_GROUP").toString());
            info.setRemark(t.get("DESCRIPTION") == null ? "" : t.get("DESCRIPTION").toString());
            info.setPrevTime(t.get("PREV_FIRE_TIME") == null ? "" : t.get("PREV_FIRE_TIME").toString());
            info.setNextTime(t.get("NEXT_FIRE_TIME") == null ? "" : t.get("NEXT_FIRE_TIME").toString());
            info.setStatus(t.get("TRIGGER_STATE") == null ? "" : t.get("TRIGGER_STATE").toString());
            info.setStartTime(t.get("START_TIME") == null ? "" : t.get("START_TIME").toString());
            info.setCron(t.get("CRON_EXPRESSION") == null ? "" : t.get("CRON_EXPRESSION").toString());
            list.add(info);
        }

        return list;
    }

    public Schedule findTriggers(String jobName, String jobGroup)
    {
        String sql = "select t.JOB_NAME, t.JOB_GROUP, t.DESCRIPTION ,t.PREV_FIRE_TIME ,t.NEXT_FIRE_TIME,t.TRIGGER_STATE ,t.START_TIME,ct.CRON_EXPRESSION,j.JOB_CLASS_NAME,j.JOB_DATA "
                + " from QRTZ_TRIGGERS t left join QRTZ_CRON_TRIGGERS ct on t.SCHED_NAME = ct.SCHED_NAME and t.TRIGGER_NAME = ct.TRIGGER_NAME "
                + " and t.TRIGGER_GROUP = ct.TRIGGER_GROUP  left join QRTZ_JOB_DETAILS j on t.JOB_NAME = j.JOB_NAME and t.JOB_GROUP = j.JOB_GROUP " + " where t.JOB_NAME = ? and t.JOB_GROUP = ?";
        Map<String, Object> trigger = template.queryForMap(sql, new Object[]
        { jobName, jobGroup });
        Schedule info = new Schedule();
        info.setJobName(trigger.get("JOB_NAME") == null ? "" : trigger.get("JOB_NAME").toString());
        info.setJobGroup(trigger.get("JOB_GROUP") == null ? "" : trigger.get("JOB_GROUP").toString());
        info.setRemark(trigger.get("DESCRIPTION") == null ? "" : trigger.get("DESCRIPTION").toString());
        info.setPrevTime(trigger.get("PREV_FIRE_TIME") == null ? "" : trigger.get("PREV_FIRE_TIME").toString());
        info.setNextTime(trigger.get("NEXT_FIRE_TIME") == null ? "" : trigger.get("NEXT_FIRE_TIME").toString());
        info.setStatus(trigger.get("TRIGGER_STATE") == null ? "" : trigger.get("TRIGGER_STATE").toString());
        info.setStartTime(trigger.get("START_TIME") == null ? "" : trigger.get("START_TIME").toString());
        info.setCron(trigger.get("CRON_EXPRESSION") == null ? "" : trigger.get("CRON_EXPRESSION").toString());
        info.setBeanClass(trigger.get("JOB_CLASS_NAME") == null ? "" : trigger.get("JOB_CLASS_NAME").toString());
        byte[] b = (byte[]) trigger.get("JOB_DATA");
        JobDataMap jobDataMap = null;
        if (b != null)
        {
            jobDataMap = (JobDataMap) PlatformUtils.toObject(b);
        }
        if (jobDataMap != null)
        {
            info.setIp(jobDataMap.getString("ip"));
            info.setPort(jobDataMap.getString("port"));
        }

        return info;
    }

}
